import { ref ,onMounted} from 'vue'
import {  onBeforeRouteUpdate } from 'vue-router'
import { useRoute } from 'vue-router';
import { getTopCategoryAPI } from '@/apis/category/category';

export function useCategoryList() {
    const route = useRoute()
    const category = ref({})
    const getCategoryForTop = async (id) => {
        const res = await getTopCategoryAPI(id)
        category.value = res.result
    }
    //钩子函数
    onMounted(async () => {
        getCategoryForTop(route.params.id)
    })

    //当路由变化时 选择精细化控制 更新分类
    onBeforeRouteUpdate((to) => {
        getCategoryForTop(to.params.id)
    })
    return {
        category
    }
}